package kr.pe.johnkim.chattingday.mobile.rest.controller;

import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayCensorshipWord;
import kr.pe.johnkim.chattingday.mobile.rest.service.ChattingDayCensorshipWordService;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Chatting Day 검열 단어 관련 Controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 5. 31
 */

@Controller
@Path("/chattingDayCensorshipWordController")
public class ChattingDayCensorshipWordController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(ChattingDayCensorshipWordController.class);

    @Autowired
    private ChattingDayCensorshipWordService chattingDayCensorshipWordService = null;

    /**
     * 검열 단어 전체 목록을 가져 온다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectAllCensorshipWordList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<ChattingDayCensorshipWord> selectAllCensorshipWordList(InputStream is) throws Exception {
        logger.info("selectAllCensorshipWordList");

//        BufferedReader br = new BufferedReader(new InputStreamReader(is));
//        String line = br.readLine();
//
//        logger.info("line : {}", line);
//
//        JSONObject jsonCensorshipWord = new JSONObject(line);
//        JSONObject jsonCensorshipWord2 = jsonCensorshipWord.getJSONObject("chattingDayCensorshipWord");

        return chattingDayCensorshipWordService.selectAllCensorshipWordList();
    }

    /**
     * 매핑된 검열 단어 목록을 가져 온다.
     *
     * Like 검색.
     * 
     * @param is
     * @return
     * @throws Exception
     */
    @POST
    @Path("/selectSearchCensorshipWordList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<ChattingDayCensorshipWord> selectSearchCensorshipWordList(InputStream is) throws Exception {
        logger.info("selectSearchCensorshipWordList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonCensorshipWord = new JSONObject(line);
        JSONObject jsonCensorshipWord2 = jsonCensorshipWord.getJSONObject("chattingDayCensorshipWord");

        ChattingDayCensorshipWord parameterCensorshipWord = new ChattingDayCensorshipWord();

        parameterCensorshipWord.setWord(jsonCensorshipWord2.getString("word"));
        parameterCensorshipWord.setActivateYn(jsonCensorshipWord2.getString("activateYn"));

        logger.info("word       : {}", parameterCensorshipWord.getWord());
        logger.info("activateYn : {}", parameterCensorshipWord.getActivateYn());

        return chattingDayCensorshipWordService.selectSearchCensorshipWordList(parameterCensorshipWord);
    }
}
